using System;
using System.Collections.Generic;
using Hematology.Data.Interface;

namespace Hematology.Data
{
	public partial class SiteContent : IComparable<SiteContent>, IEquatable<SiteContent>, ISimpleData
	{
		public string Teaser { get; set; }
		public SiteContentType ContentType { get; private set; }
		public List<SiteContent> ChildSiteContent { get; private set; }
		public List<FileAttach> FileAttachements { get; set; }

		public void Save()
		{
			IsDeleted = false;
			CreateDate = DateTime.Now;
			LastModifiedDate = DateTime.Now;

			Insert();
		}

		public void UpdateInstance()
		{
			LastModifiedDate = DateTime.Now;

			Update();
		}

		public void MarkAsDeleted()
		{
			IsDeleted = true;

			Update();
		}

		private void GetContentType()
		{
			if (ContentTypeCode.HasValue)
				ContentType = SiteContentType.GetSiteContentTypeByCode(ContentTypeCode.Value);
		}

		private void FillFileAttachList()
		{
			if (SiteContentId.HasValue)
				FileAttachements = new List<FileAttach>(FileAttach.ListBySiteContentId(SiteContentId.Value));
		}

		private void FillChildSiteContent()
		{
			if (SiteContentId.HasValue)
				ChildSiteContent = Search(new SiteContent
											{
												ParentSiteContentId = SiteContentId,
												IsDeleted = false
											});
		}

		public static List<SiteContent> ListAllContent(int? iContentType, bool bIsOnlyVisible, bool bIsOnlyParents)
		{
			List<SiteContent> lSiteContent = Search(
				new SiteContent
					{
						ContentTypeCode = iContentType,
						IsDeleted = false,
						IsVisible = bIsOnlyVisible ? true : (bool?) null,
					});

			if (lSiteContent != null && bIsOnlyParents)
				lSiteContent.RemoveAll(siteContent => siteContent.ParentSiteContentId.HasValue);

			return lSiteContent;
		}

		public int CompareTo(SiteContent other)
		{
			return Title.CompareTo(other.Title);
		}

		public bool Equals(SiteContent other)
		{
			return SiteContentId == other.SiteContentId;
		}
	}
}